﻿using Mrsoft.Model;
using System;
using System.IO;
using System.Xml.Serialization;

namespace Mrsoft.Core {

    public class ZhongZhiInfo {
        /// <summary>
        /// 上次种下的时间
        /// </summary>
        public DateTime LastZhongXiaTime;

        /// <summary>
        /// 上次种植熟制
        /// </summary>
        public ZhongZhiShuZhi LastZhongZhiShuZhi;

        /// <summary>
        /// 默认种植熟制
        /// 需要设置
        /// </summary>
        public ZhongZhiShuZhi DefaultZhongZhiShuZhi;

        /// <summary>
        /// 上次种植的作物
        /// 自动保存
        /// </summary>
        public string LastZhongZhiZuoWu;

        /// <summary>
        /// 种植作物
        /// 配置
        /// </summary>
        public string DefaultZhongZhiZuoWu;

        /// <summary>
        /// 上次种植的地图
        /// 自动保存
        /// </summary>
        public string LastZhongZhiMap;

        /// <summary>
        /// 默认种植地图
        /// 配置文件
        /// </summary>
        public string DefaultZhongZhiMap;

        /// <summary>
        /// 自动保存
        /// 上次种植的坐标位置
        /// </summary>
        public XY LastZhongZhiXY;

        /// <summary>
        /// 是否自动转种晚产
        /// 配置属性
        /// </summary>
        public bool AutoChangetZaoToWan;

        /// <summary>
        /// 最少精力值转种用
        /// 配置属性
        /// </summary>
        public int LowestEnergy;

        /// <summary>
        /// 静态创建
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static ZhongZhiInfo FromXml(string userName) {
            string file = string.Format("./user/{0}.zz.xml", userName);
            ZhongZhiInfo result = null;
            if (!XmlSaver<ZhongZhiInfo>.FromXml(file, out result)) {
                result = NewThisObject(userName);
            }
            return result;
        }

        /// <summary>
        /// 新建一个对象并保存基本信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private static ZhongZhiInfo NewThisObject(string userName) {
            var result = new ZhongZhiInfo {
                AutoChangetZaoToWan = true,
                DefaultZhongZhiShuZhi = ZhongZhiShuZhi.晚产,
                DefaultZhongZhiZuoWu = "彩棉",
                DefaultZhongZhiMap = "苏州",

                LastZhongXiaTime = DateTime.Now.AddDays(-1),
                LastZhongZhiMap = "苏州",
                LastZhongZhiXY = new XY(),
                LastZhongZhiShuZhi = ZhongZhiShuZhi.晚产,
                LastZhongZhiZuoWu = "彩棉",
                LowestEnergy = 50
            };
            result.Save(userName);
            return result;
        }

        /// <summary>
        /// 将对象序列化保存起来
        /// </summary>
        /// <param name="userName"></param>
        public bool Save(string userName) {
            string file = string.Format("./user/{0}.zz.xml", userName);
            return XmlSaver<ZhongZhiInfo>.Save(file, this);
        }
    }
}